import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// 静态路由
export const constantRoutes = [
  // login 登录
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  // 404
  {
    path: '/404',
    component: () => import('@/views/404.vue'),
    hidden: true
  },
  // dashboard 首页
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard', // 重定向
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index.vue'),
      meta: { title: '首页', icon: 'dashboard' } // 路由原信息 会将meta里面的信息展示在左边菜单栏
    }]
  }
  // 兜底
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

// 动态路由 让有权限的人 看到动态路由
export const asyncRoutes = [
  // department 组织架构
  {
    path: '/department',
    name: 'department',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/department/index.vue'), meta: { title: '组织', icon: 'tree' }}
    ]
  },
  // role 角色管理
  {
    path: '/role',
    name: 'role',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/role/index.vue'), meta: { title: '角色', icon: 'setting' }},
      { path: '/role/assign/:id?', component: () => import('@/views/role/assign-permission.vue'), name: 'roleassign', hidden: true, meta: { title: '角色-分配权限' }}
    ]
  },
  // employee 员工管理
  {
    // hidden可以在左侧菜单隐藏
    path: '/employee',
    name: 'employee',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/employee/index.vue'), meta: { title: '员工', icon: 'people' }},
      { path: '/employee/detail/:id?', component: () => import('@/views/employee/detail.vue'), name: 'employeedetail', hidden: true, meta: { title: '员工详情' }},
      { path: '/employee/assign/:id?', component: () => import('@/views/employee/assign-role.vue'), name: 'employeeassign', hidden: true, meta: { title: '分配-员工-角色' }}
    ]
  },
  // permission 权限管理
  {
    path: '/permission',
    name: 'permission',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/permission/index.vue'), meta: { title: '权限', icon: 'lock' }}
    ]
  },
  // attendance 考勤管理
  {
    path: '/attendance',
    name: 'attendance',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/attendance/index.vue'), name: 'attendance', meta: { title: '考勤', icon: 'excel' }}
    ]
  },
  // approval 审批管理
  {
    path: '/approval',
    name: 'approval',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/approval/index.vue'), name: 'approval', meta: { title: '审批', icon: 'tree-table' }}
    ]
  },
  // salary 工资管理
  {
    path: '/salary',
    name: 'salary',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/salary/index.vue'), name: 'salary', meta: { title: '工资', icon: 'money' }},
      { path: '/salary/detail/:yearMonth/:id', component: () => import('@/views/salary/detail.vue'), hidden: true, name: 'salarydetail', meta: { title: '个人工资详情' }}
    ]
  },
  // social 社保管理
  {
    path: '/social',
    name: 'social',
    component: Layout,
    children: [
      { path: '', component: () => import('@/views/social/index.vue'), name: 'social', meta: { title: '社保', icon: 'table' }},
      { path: '/social/detail/:id?', component: () => import('@/views/social/detail.vue'), name: 'socialdetail', hidden: true, meta: { title: '社保详情' }}
    ]
  }
]

const createRouter = () => new Router({
  mode: 'history', // require service support 开放环境不刷新 webpack配置
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes // 默认引用静态路由
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
